package com.example.springboot_sdse_air.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot_sdse_air.entity.ChatMessage;
import com.example.springboot_sdse_air.entity.ChatSession;
import com.example.springboot_sdse_air.mapper.ChatMessageMapper;
import com.example.springboot_sdse_air.mapper.ChatSessionMapper;
import com.example.springboot_sdse_air.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * 聊天服务实现类
 * 功能：处理聊天会话和消息的数据库操作
 * 作者：王彬 202200111149
 * 日期：2025-01-14
 */
@Service
@RequiredArgsConstructor
public class ChatServiceImpl extends ServiceImpl<ChatSessionMapper, ChatSession> implements ChatService {

    private final ChatMessageMapper chatMessageMapper;

    @Override
    @Transactional
    public Integer createChatSession(Integer uid, String title, Integer pdfId) {
        ChatSession session = new ChatSession();
        session.setUid(uid);
        session.setTitle(title);
        session.setPdfId(pdfId);
        session.setState("active");
        session.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        
        // 保存会话
        save(session);
        return session.getChatId();
    }

    @Override
    @Transactional
    public Integer saveChatMessage(Integer chatId, String sender, String message) {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setChatId(chatId);
        chatMessage.setSender(sender);
        chatMessage.setMessage(message);
        chatMessage.setTimestamp(new Timestamp(System.currentTimeMillis()));
        
        // 保存消息
        chatMessageMapper.insert(chatMessage);
        return chatMessage.getMsgId();
    }

    @Override
    public List<ChatMessage> getChatMessages(Integer chatId) {
        QueryWrapper<ChatMessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chat_id", chatId)
                   .orderByAsc("timestamp");
        return chatMessageMapper.selectList(queryWrapper);
    }

    @Override
    public List<ChatSession> getUserChatSessions(Integer uid) {
        QueryWrapper<ChatSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid)
                   .orderByDesc("`update`");
        return list(queryWrapper);
    }

    @Override
    public ChatSession getLatestActiveSessionByPdf(Integer uid, Integer pdfId) {
        QueryWrapper<ChatSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid)
                   .eq("pdf_id", pdfId)
                   .eq("state", "active")
                   .orderByDesc("`update`")
                   .last("LIMIT 1");
        return getOne(queryWrapper);
    }

    @Override
    @Transactional
    public void updateChatSessionState(Integer chatId, String state) {
        ChatSession session = getById(chatId);
        if (session != null) {
            session.setState(state);
            session.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            updateById(session);
        }
    }
} 